/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wiView.graphics.GraphObject;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.SwingUtilities;
import org.graphstream.graph.Node;
import org.graphstream.ui.geom.Point3;
import org.graphstream.ui.graphicGraph.GraphicElement;
import org.graphstream.ui.graphicGraph.GraphicGraph;
import org.graphstream.ui.j2dviewer.J2DGraphRenderer;
import org.graphstream.ui.spriteManager.Sprite;
import org.graphstream.ui.swingViewer.View;
import org.graphstream.ui.swingViewer.Viewer;
import org.graphstream.ui.swingViewer.util.Camera;
import org.graphstream.ui.swingViewer.util.DefaultMouseManager;
import org.graphstream.ui.swingViewer.util.MouseManager;

/**
 *
 * @author Hoang
 */
public class WiView implements MouseWheelListener{
   
    protected View view;
    protected WiGraph graph;
    double press_x = 0;
    double press_y = 0;
    double current_center_x;
    double current_center_y;
    double drag_x = 0;
    double drag_y = 0;
    double previous_drag_x = 0;
    double previous_drag_y = 0;
    double click_x = 0;
    double click_y = 0;
    boolean isRelease = false;
    public WiView(WiGraph graph, View view) {
        this.view = view;
        this.graph = graph;
        this.current_center_x = view.getCamera().getViewCenter().x;
        this.current_center_y = view.getCamera().getViewCenter().y;
     //   this.view = this.viewer.addDefaultView(false);
    }
    
    
    public void setMouseListener(){
        view.addMouseWheelListener(this);
        view.setMouseManager(new MouseManager() {

            @Override
            public void init(GraphicGraph gg, View view) {
                view.addMouseListener(this);
                view.addMouseMotionListener(this);
            }
            @Override
            public void release() {
                //throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public void mouseClicked(MouseEvent me) {
                if(me.getClickCount() == 2){
                    Point p = me.getPoint();
                    GraphicElement e = view.findNodeOrSpriteAt(p.getX(), p.getY());
                    if(e != null)
                        System.out.println(e.getId());
                }
                
            }

            @Override
            public void mousePressed(MouseEvent me) {
                //throw new UnsupportedOperationException("Not supported yet.");
                
                 press_x = Double.parseDouble(Integer.toString(me.getX()));
                 press_y = Double.parseDouble(Integer.toString(me.getY()));
                 previous_drag_x = press_x;
                 previous_drag_y = press_y;
                 
                 isRelease = false;
                 
            }

            @Override
            public void mouseReleased(MouseEvent me) {
                //throw new UnsupportedOperationException("Not supported yet.");
                 if(SwingUtilities.isRightMouseButton(me)){
                 System.out.println("onRightClicked"); 
                // showPopup(me);
                 
                }
                 else if(SwingUtilities.isLeftMouseButton(me)){
                handleMoveGraph(press_x, press_y, me.getX(), me.getY(), view);
                isRelease = true;
                 }
                
            }

            @Override
            public void mouseEntered(MouseEvent me) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public void mouseExited(MouseEvent me) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public void mouseDragged(MouseEvent me) {
                //throw new UnsupportedOperationException("Not supported yet.");
                
                if(SwingUtilities.isLeftMouseButton(me)){
                drag_x = Double.parseDouble(Integer.toString(me.getX()));
                drag_y = Double.parseDouble(Integer.toString(me.getY()));
                if(isRelease == false){
                    handleMoveGraph(press_x,press_y,drag_x,drag_y,view);
                }
                }
            }

            @Override
            public void mouseMoved(MouseEvent me) {
                //throw new UnsupportedOperationException("Not supported yet.");
                
                
            }/*
             private void showPopup(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    popupmenu.show(e.getComponent(),
                            e.getX(), e.getY());
                }
            }
            * */
            private void handleMoveGraph(double press_x, double press_y, double drag_x, double drag_y, View view) {
                current_center_x = current_center_x -(( drag_x - previous_drag_x ) / 10);
                current_center_y = current_center_y + ( drag_y - previous_drag_y ) / 10;
                previous_drag_x = drag_x;
                previous_drag_y = drag_y;
                view.getCamera().setViewCenter(current_center_x, current_center_y, 0);
            }
        });
    }

    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        int rot = e.getWheelRotation();

	Camera c = view.getCamera();
	double p = c.getViewPercent();
	if (rot > 0) {
	    if (p < 1.5) {
		c.setViewPercent(p + 0.1);
	    }
	} else {
	    if (p > 0.2) {
		c.setViewPercent(p - 0.1);
	    }
	}

	e.consume();
    }
    
    
}
